package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.model.bo.mall.*;
import com.cskaoyan.mall.model.vo.mall.GetOrderByStateVO;
import com.cskaoyan.mall.model.vo.mall.SignUpVO;
import com.cskaoyan.mall.service.mall.MallOrdersService;
import com.cskaoyan.mall.service.mall.MallOrdersServiceImpl;
import com.cskaoyan.mall.service.mall.MallUserService;
import com.cskaoyan.mall.service.mall.MallUserServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.cskaoyan.mall.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

@WebServlet("/api/mall/order/*")
public class MallOrdersServlet extends HttpServlet {
    private Gson gson = new Gson();
    private MallOrdersService mallOrdersService = new MallOrdersServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/","");
        //api/mall/user/signup
        if ("addOrder".equals(action)) {
            addOrder(request,response);
        }else if ("settleAccounts".equals(action)) {
            settleAccounts(request,response);
        }else if ("sendComment".equals(action)) {
            sendComment(request,response);
        }
    }



    /**
     * 商品评价
     *
     * @param request
     * @param response
     */
    private void sendComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        //对请求报文进行处理
        SendCommentBO comment = gson.fromJson(requestBody, SendCommentBO.class);
        int code = mallOrdersService.sendComment(comment);;
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("评价失败")));
    }

    /**
     * 结算，吧购物车的订单转为未发货
     * @param request
     * @param response
     */
    private void settleAccounts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        //对请求报文进行处理
        CartListBO cart = gson.fromJson(requestBody, CartListBO.class);
        int code = mallOrdersService.settleAccounts(cart);;
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("结算失败")));
    }

    /**
     * 添加订单 立即购买/加入购物车
     * {token: "admin", goodsDetailId: 1853, state: 0, num: 1, amount: 11}
     * 返回code0
     * @param request
     * @param response
     */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        //对请求报文进行处理
        AddOrderBO addOrderBO = gson.fromJson(requestBody, AddOrderBO.class);
        int code = mallOrdersService.addOrder(addOrderBO);;
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("加入购物车失败")));
    }

    /**
     *根据传来的参数token以及state进行展示
     * {"code":0,"data":
     * [{"id":2323,
     * "state":0,
     * "goodsNum":1,
     * "amount":200.0,
     * "goodsDetailId":1238,"
     * createtime":"2020-08-12 16:28:33","
     * hasComment":false,
     * "goods":{"id":478,"img":"http://115.29.141.32:8084/static/image/157123055832115669931721908195203.jpg",
     * "name":"新品外套",
     * "goodsDetailId":1238,
     * "spec":"卡其色",
     * "unitPrice":200.0}}]}
     * @param request
     * @param response
     */
    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        String state = request.getParameter("state");
        List<GetOrderByStateVO> getOrderByStateVO = mallOrdersService.getOrderByState(token,state);
        Gson gson1 = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        response.getWriter().println(gson1.toJson(Result.ok(getOrderByStateVO)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doget");
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/","");
        if ("getOrderByState".equals(action)) {
            getOrderByState(request,response);
        }else if ("deleteOrder".equals(action)) {
            deleteOrder(request,response);
        }else if ("confirmReceive".equals(action)) {
            confirmReceive(request,response);
        }else if ("pay".equals(action)) {
            pay(request,response);
        }
    }

    /**
     * 在订单中确认付款
     * @param request
     * @param response
     */
    private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int code = mallOrdersService.pay(id);;
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("支付失败")));
    }

    /**
     * 将已发货变为完成state设置为3
     * @param request
     * @param response
     */
    private void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int code = mallOrdersService.confirmReceive(id);;
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("订单未完成")));
    }

    /**
     * 在购物车中删除订单
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int code = mallOrdersService.deleteOrder(id);;
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("删除失败")));
    }
}
